package org.starapp.utils;

import java.io.ByteArrayOutputStream;

public class StringUtils {
	
	public static String convent2javaFormat(String dbField){
		return convent2javaFormat(null,dbField);
	}
	
	public static String convent2javaLowerFormat(String dbField){
		return convent2javaLowerFormat(null, dbField);
	}
	
	public static String convent2javaLowerFormat(String prefix,String dbField){
		if(!isEmpty(prefix)&&dbField.length()>prefix.length()
				&&dbField.startsWith(prefix)){
			dbField=dbField.substring(prefix.length());
		}
		//查看是否有"_"
		String str=convent2javaFormat(dbField);
		return str.substring(0,1).toLowerCase()+str.substring(1);	
	}


	public static String convent2javaFormat(String prefix,String dbField){
		if(!isEmpty(prefix)&&dbField.length()>prefix.length()
				&&dbField.startsWith(prefix)){
			dbField=dbField.substring(prefix.length());
		}
		//查看是否有"_"
		StringBuffer sb=new StringBuffer();
		String[] fields=null;
		if(dbField==null){
			return null;
		}
		dbField=dbField.toLowerCase();
		if(dbField.indexOf("_")!=-1){
			fields = dbField.split("_");
		}else{
			sb.append(dbField.substring(0,1).toUpperCase()).append(dbField.substring(1));
			return sb.toString();
		}
		String temp="";
		if(fields!=null && fields.length>0){
			for(int i=0;i<fields.length;i++){
				temp=fields[i];
				if(temp.trim().equals("")){
					continue;
				}
				sb.append(temp.substring(0,1).toUpperCase()+temp.substring(1));
			}
		}
		return sb.toString();
	}
	public static String convent2javaFunctionFormat(String dbField){
		String temp=convent2javaFormat(dbField);
		if(!isEmpty(temp)){
			temp=temp.substring(0,1).toUpperCase()+temp.substring(1);
		}
		return temp;
	}
	
	public static boolean isEmpty(String s){
		return (s==null||s.trim().equals(""))?true:false;
	}
	
	public static boolean isabc(String string)
	{
      	boolean result=false;
      	if(string==null||string.equals(""))
      	{
      		throw new RuntimeException("isabc函数参数不能为空"+Number.class.getName());
      	}
      	byte[] chars=string.getBytes();
      	for(int i=0;i<chars.length;i++)
      	{
      		if((int)chars[i]>=97||(int)chars[i]<=122)
      		{
      			continue;
      		}else
      		{
      			result=true;
      			break;
      		}
      	}
      	return result;	
	}
	public static boolean isABC(String string)
	{
      	boolean result=false;
      	if(string==null||string.equals(""))
      	{
      		throw new RuntimeException("isABC函数参数不能为空"+Number.class.getName());
      	}
      	byte[] chars=string.getBytes();
      	for(int i=0;i<chars.length;i++)
      	{
      		if((int)chars[i]>=65||(int)chars[i]<=90)
      		{
      			continue;
      		}else
      		{
      			result=true;
      			break;
      		}
      	}
      	return result;	
	}
	/*左补0*/
	public static String leftZero(String str,int length){
		if(str.trim().length()<length){
			int n=length-str.trim().length();
			for(int i=0;i<n;i++){
				str="0"+str;
			}
		}
		return str;
	}
	/*左补空格*/
	public static String leftBlank(String str,int length){
		if(str.trim().length()<length){
			int n=length-str.trim().length();
			for(int i=0;i<n;i++){
				str=" "+str;
			}
		}
		return str;
	}
	/*右补0*/
	public static String rightZero(String str,int length){
		if(str.trim().length()<length){
			int n=length-str.trim().length();
			for(int i=0;i<n;i++){
				str=str+"0";
			}
		}
		return str;
	}
	/*右补空格*/
	public static String rightBlank(String str,int length){
		if(str.trim().length()<length){
			int n=length-str.trim().length();
			for(int i=0;i<n;i++){
				str=str+" ";
			}
		}
		return str;
	}
	/**
	 * 判断是否为空
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str){
		return (str==null||str.equals("")||(str.length()>0&&str.trim().length()==0));
	}
	
	public static boolean isNotBlank(String str){
		return (str!=null&&!str.equals("")&&str.trim().length()>0);
	}
	
	public static String[] split(String str,String split){
		return str.split(split);
	}
	
	public static String[] split(String str,String split,int index){
		return str.split(split, index);
	}
	
	public static String remove(String str,String split){
		return str.replaceAll(split, "");
	}
	
	public static String StringsToString(String[] strs) {
		String result = "";
		if (strs != null && strs.length > 0) {
			for (int i = 0; i < strs.length; i++) {
				result += strs[i].toString();
			}
		}
		return result;
	}

	public static Object NullToString(Object object) {
		return object == null ? "" : object;
	}

	public static String bytesToString(byte[] encrytpByte) {
		String result = "";
		result = encrytpByte.toString();
		return result;
	}

	public static String bytesToHexString(byte[] in) {
		String result = "";
		if (in != null) {
			for (int i = 0; i < in.length; i++) {
				result += Integer.toHexString(in[i] & 0xFF);
			}
		} else {
			throw new RuntimeException("err!,param is null.please input param!");
		}
		return result;
	}

	public static byte[] hexStringToBytes(String in) {
		byte[] result = null;
		result = in.getBytes();
		if (in != null) {
			result = new byte[in.length() / 2];
			System.out.println(in.length());
		} else {
			throw new RuntimeException("err!,param is null.please input param!");
		}
		return result;
	}

	public static String intToBinaryString(int in) {
		String result = Integer.toBinaryString(in);
		if (result.length() <= 32) {
			for (int i = result.length() + 1; i <= 32; i++) {
				result = "0" + result;
			}
		}
		return result;
	}

	public static String intsToBinaryString(byte[] in) {
		String result = "";
		if (in == null) {
			throw new RuntimeException("error ! reason maybe int[] is null");
		}
		for (int i = 0; i < in.length; i++) {
			if (i == 0) {
				result = intToBinaryString(in[i]);
			} else {
				result = result + " " + intToBinaryString(in[i]);
			}
		}
		return result;
	}

	public static String bytesToFormatBinaryText(byte[] in) {
		String result = "";
		for (int i = 0; i < in.length / 2; i++) {

		}
		return result;
	}

	public static String bytesToFormatHexText(byte[] in) {
		String result = "";

		return result;
	}

	public static int bytes2int(byte[] b) {
		// byte[] b=new byte[]{1,2,3,4};
		int mask = 0xff;
		int temp = 0;
		int res = 0;
		for (int i = 0; i < 4; i++) {
			res <<= 8;
			temp = b[i] & mask;
			res |= temp;
		}
		return res;
	}

	public static byte[] int2bytes(int num) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			b[i] = (byte) (num >>> (24 - i * 8));
		}
		return b;
	}

	/*
	 * 将字符串编码成16进制数字,适用于所有字符（包括中文）
	 */
	public static String encode(String str) {
		String hexString = "0123456789abcdef";
		byte[] bytes = str.getBytes(); // 根据默认编码获取字节数组
		StringBuilder sb = new StringBuilder(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) // 将字节数组中每个字节拆解成2位16进制整数
		{
			sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
			sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
		}
		return sb.toString();
	}

	/*
	 * 将16进制数字解码成字符串,适用于所有字符（包括中文）
	 */
	public static String decode(String bytes) {
		String hexString = "0123456789abcdef";
		ByteArrayOutputStream baos = new ByteArrayOutputStream(
				bytes.length() / 2);
		// 将每2位16进制整数组装成一个字节
		for (int i = 0; i < bytes.length(); i += 2)
			baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
					.indexOf(bytes.charAt(i + 1))));
		return new String(baos.toByteArray());
	}

	public static void main(String args[]) {
		System.out.println(encode("中文 "));
		System.out.println(decode(encode("中文 ")));
		System.out.println(convent2javaLowerFormat(null,"PASSWORD"));
		System.out.println(convent2javaFormat(null,"PASSWORD"));
		System.out.println(convent2javaLowerFormat(null,"PASSWORD_ID"));
		System.out.println(convent2javaFormat(null,"PASSWORD_ID"));
	}
}
